package top.suven.base.util.oldutil;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import top.suven.base.util.utilize.JsonUtils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HsSdkUtils {
	
	public final static Logger logger = (Logger) LoggerFactory.getLogger(HsSdkUtils.class);
	
	/* 分页数 **/
	private static int offset = 1000;
	
	/* 请求超时时间 **/
	private static int timeout = 3000;
	
	private static String ENCODING = "UTF-8";

	/**
	 * 用户角色信息
	 * @param userName
	 * @param gameId
	 * @param serverArea
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> getUserRoleList(String userName, Integer gameId, String serverArea) throws Exception{
		Map<String, String> params = new TreeMap<String, String>();
		params.put("clientid", HsSdk.HS_SDK_CLIENTID);
		params.put("appid", HsSdk.HS_SDK_APPID);
		params.put("username", userName);
		if(gameId != null) params.put("gameid", String.valueOf(gameId));
		if(StringUtils.isNotBlank(serverArea)) params.put("server", serverArea);
		params.put("offset", String.valueOf(offset));
		params.put("timestamp", String.valueOf(System.currentTimeMillis()/1000));
		
		String sign = HttpEncryptUtils.getSignVeryfyCode(params, "key", HsSdk.HS_SDK_CLIENTKEY, ENCODING);
		params.put("sign", sign);
		
		String url = HsSdk.HS_SDK_URL + "/api/public/v1/user/role/list?"+HttpEncryptUtils.createLinkString(params);
		logger.info("userName={} hs sdk request url:{}", userName, url);
		
		HttpClient httpClient = new HttpClient(url, timeout, timeout);
		int responseCode = httpClient.sendGet(ENCODING);
		if(responseCode == 200){
			return JsonUtils.readValue(httpClient.getResult(), HashMap.class);
		}else {
			throw new RuntimeException("getUserRoleList http request data error, error code:"+responseCode);
		}
	}
	
	/**
	 * 用户充值总金额
	 * @param userName
	 * @param gameId
	 * @param serverArea
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> getUserRechargeMoney(String userName, Integer gameId, String serverArea) throws Exception{
		Map<String, String> params = new TreeMap<String, String>();
		params.put("clientid", HsSdk.HS_SDK_CLIENTID);
		params.put("appid", HsSdk.HS_SDK_APPID);
		params.put("username", userName);
		if(gameId != null) params.put("gameid", String.valueOf(gameId));
		if(StringUtils.isNotBlank(serverArea)) params.put("server", serverArea);
		params.put("timestamp", String.valueOf(System.currentTimeMillis()/1000));
		
		String sign = HttpEncryptUtils.getSignVeryfyCode(params, "key", HsSdk.HS_SDK_CLIENTKEY, ENCODING);
		params.put("sign", sign);
		
		String url = HsSdk.HS_SDK_URL + "/api/public/v1/user/money/read?"+HttpEncryptUtils.createLinkString(params);
		logger.info("userName={} hs sdk request url:{}", userName, url);
		
		HttpClient httpClient = new HttpClient(url, timeout, timeout);
		int responseCode = httpClient.sendGet(ENCODING);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(responseCode == 200){
			return JsonUtils.readValue(httpClient.getResult(), HashMap.class);
		}else {
			throw new RuntimeException("getUserRechargeMoney http request data error, error code:"+responseCode);
		}
	}
	
	/**
	 * 赠送红币
	 * @param userName
	 * @param amount
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> giveptb(String userName, Integer amount) throws Exception{
		Map<String, String> params = new TreeMap<String, String>();
		params.put("username", userName);
		params.put("amount", String.valueOf(amount));
		
		String keyStr = userName + HsSdk.HS_SDK_CLIENTKEY + amount;
		String sign = MD5Utils.MD5Encode(keyStr, ENCODING);
		params.put("sign", sign);
		
		String url = HsSdk.HS_SDK_URL + "/sdk/ptbpay/giveptb.php";
		logger.info("userName={} hs sdk request url:{}", userName, url);
		
		HttpClient httpClient = new HttpClient(url, timeout, timeout);
		int responseCode = httpClient.sendPost(params, ENCODING);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(responseCode == 200){
			return JsonUtils.readValue(httpClient.getResult(), HashMap.class);
		}else {
			throw new RuntimeException("giveptb http request data error, error code:"+responseCode);
		}
	}
	
	public static void main(String[] args) {
		try {
			HsSdk.HS_SDK_URL = "http://g.gc.com.cn";
			HsSdk.HS_SDK_CLIENTID = "17";
			HsSdk.HS_SDK_APPID = "100";
			HsSdk.HS_SDK_CLIENTKEY="dc84ca832a9839a338acae806e34a268";
			Map<String, Object> resultMap = giveptb("17001352209", 5);
			System.out.println(JsonUtils.toJsonString(resultMap));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
